import 'package:amap_flutter_map/amap_flutter_map.dart';
import '../../../network/http_request.dart';
import 'package:get/get.dart';
import '../Home/map/canteen.dart';
import 'model/shop_matchmaker_list_model.dart';
import 'package:amap_flutter_base/amap_flutter_base.dart';

class HomeMainController extends GetxController {
  List<Marker> markList = [];
  double starLat = 0; //当前位置纬度
  double starLon = 0; //当前位置经度
  String detailStr = '北京朝阳';
  String saddress = ''; //起点地址
  String showPermissionViewId = 'showPermissionViewId';
  bool isShowPermission=false;

  getMapShopList(longitude,latitude,) async {
    var res = await HttpRequest.request('match-maker/business/querys',
        params: {'nearbyLon': longitude, 'nearbyLat': latitude});
    ShopMatchMakerListModel shopMatchMakerListModel =
        ShopMatchMakerListModel.fromJson(res);
    List<Marker> resList = mapMarkList(shopMatchMakerListModel.data ?? []);
    if(markList.isNotEmpty){
      markList.clear();
    }
    markList.addAll(resList);
  }

  List<Marker> mapMarkList(List<ShopMatchMakerListDataModel> shopList) {
    List<Marker> markers = [];
    for (ShopMatchMakerListDataModel element in shopList) {
      Marker pointMarker = Marker(
        position: LatLng(element.latitude, element.longitude),
        icon: BitmapDescriptor.fromIconPath(getCompanyType(element)),
        infoWindow: InfoWindow(
          title: element.area,
          snippet: '',
        ),
        onTap: (marker) {
          CustomShareView().showShareView(Get.context!,
              dataModel: element,
              locationPoint: LatLng(
                starLat,
                starLon,
              ),
              saddress: saddress);
        },
      );
      markers.add(pointMarker);
    }
    return markers;
  }

  @override
  void onReady() {
    super.onReady();
  }

  String getCompanyType(ShopMatchMakerListDataModel model) {
    String iconStr = "images/ic_mapIcon4.png";
    switch (model.trade) {
      case '餐饮':
        iconStr = "images/ic_mapIcon4.png";
        break;
      case '健身房':
        iconStr = "images/ic_mapIcon3.png";
        break;
      case '影院':
        iconStr = "images/ic_mapIcon1.png";
        break;
      case '咖啡厅':
        iconStr = "images/ic_mapIcon2.png";
        break;
      case '酒吧':
        iconStr = "images/ic_mapIcon5.png";
        break;
    }

    return iconStr;
  }
}
